package com.fzx.fzwsupplysystem.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.fzx.fzwsupplysystem.R;
import com.fzx.fzwsupplysystem.SplashActivity;
import com.fzx.fzwsupplysystem.view.CustomDialog;

/**
 * 项目：FzwSupplySystem
 * 创建人：FangZhiXin
 * 时间：2019/11/15 15:36
 * 描述：
 */
public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这里可以把所有页面加进一个栈，
    }

    public void gotoHome() {
        //回到启动页
        Intent i = new Intent(this, SplashActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }

    public void gotoAc(Class cls) {
        startActivity(new Intent(this, cls));
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    public void finishAc() {
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    private CustomDialog customDialog;

    protected void showMessageDialog(String title, String msg, boolean canceled, final boolean toHome) {
        hideMessageDialog();
        customDialog = new CustomDialog(this, 400, 200, R.style.DialogStyle);
        customDialog.setBackgroundResource(R.color.colorWhite);
        customDialog.setTitle(title);
        customDialog.setMessage(msg);
        customDialog.setCanceledOnTouchOutside(canceled);
        customDialog.setButtonBackground(R.drawable.selector_dialog_btn);
        customDialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: ");
                if (toHome) {
                    gotoHome();
                }
            }
        });
        customDialog.show();
    }

    protected void showYNDialog(String msg, String textY, View.OnClickListener yListener, String textN, View.OnClickListener nListener) {
        hideMessageDialog();
        customDialog = new CustomDialog(this, 400, 200, R.style.DialogStyle);
        customDialog.setBackgroundResource(R.color.colorWhite);
//        customDialog.setTitle(title);
        customDialog.setMessage(msg);
        customDialog.setCanceledOnTouchOutside(false);
        customDialog.setButtonBackground(R.drawable.selector_dialog_btn);
        customDialog.setPositiveButton(textY, yListener);
        customDialog.setNegativeButton(textN, nListener);
        customDialog.show();
    }

    protected void showDialog(String msg, View.OnClickListener yListener, View.OnClickListener nListener) {
        hideMessageDialog();
        customDialog = new CustomDialog(this, 400, 200, R.style.DialogStyle);
        customDialog.setBackgroundResource(R.color.colorWhite);
//        customDialog.setTitle(title);
        customDialog.setMessage(msg);
        customDialog.setCanceledOnTouchOutside(false);
        customDialog.setButtonBackground(R.drawable.selector_dialog_btn);
        customDialog.setPositiveButton("是", yListener);
        customDialog.setNegativeButton("否", nListener);
        customDialog.show();
    }

    protected void hideMessageDialog() {
        if (customDialog != null && customDialog.isShowing()) {
            customDialog.closeDialog();
        }
    }

    private ProgressDialog progressDialog;

    protected void showProgressDialog(boolean cancelable, String msg) {
        hideProgressDialog();
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(msg);
        progressDialog.setCanceledOnTouchOutside(cancelable);
        progressDialog.show();
    }

    protected void hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public static void hideSoftInput(View view, Context context) {
        if(view == null || context == null){
            return;
        }
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //强制隐藏键盘
//            imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideMessageDialog();
        hideProgressDialog();
    }
}
